﻿Namespace myBookmarks.Web
    Public Class ExportToHtmlResult
        Inherits ActionResult

        Private _Content As String
        Public Property Content() As String
            Get
                Return _Content
            End Get
            Set(ByVal value As String)
                _Content = value
            End Set
        End Property

        Private _ContentEncoding As Encoding
        Public Property ContentEncoding() As Encoding
            Get
                Return _ContentEncoding
            End Get
            Set(ByVal value As Encoding)
                _ContentEncoding = value
            End Set
        End Property

        Private _Name As String
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

        Public Overloads Overrides Sub ExecuteResult(ByVal context As ControllerContext)
            If context Is Nothing Then
                Throw New ArgumentNullException("context")
            End If

            Dim _response As HttpResponseBase = context.HttpContext.Response

            _response.ContentType = "text/html"

            If ContentEncoding IsNot Nothing Then
                _response.ContentEncoding = ContentEncoding
            End If

            Dim fileName = "Bookmarks.html"
            If Not [String].IsNullOrEmpty(Name) Then
                fileName = If(Name.Contains("."c), Name, Name & ".html")
            End If
            _response.AddHeader("Content-Disposition", [String].Format("attachment; filename={0}", fileName))

            If Content IsNot Nothing Then
                _response.Write(Content)
            End If
        End Sub
    End Class
End Namespace